package com.app.signing.data

import android.content.Context
import android.content.SharedPreferences
import com.app.signing.app.App
import com.app.signing.data.bean.SigningSetting
import com.app.signing.view.login.LoginStates

class SpManager private constructor() {

    companion object {

        const val SP_NAME = "signing_user"

        const val USER_ID_KEY = "user_id_key"

        const val USER_LOGIN_STATES = "user_login_states"

        const val SIGNING_LATITUDE_KEY = "SIGNING_LATITUDE_KEY"

        const val SIGNING_LONGITUDE_KEY = "SIGNING_LONGITUDE_KEY"

        const val SIGNING_RADIUS_KEY = "SIGNING_RADIUS_KEY"

        const val SIGNING_INTERVAL_KEY = "SIGNING_INTERVAL_KEY"

        const val SIGNING_ING_SP_NAME = "SIGNING_ING_SP_NAME"

        const val SIGNING_SEAT_ID_KEY = "SIGNING_SEAT_ID_KEY"

        const val SIGNING_ING_ID_KEY = "SIGNING_ING_ID_KEY"

        const val STUDY_TIME_KEY = "STUDY_TIME_KEY"

        val INSTANCE: SpManager by lazy {
            SpManager()
        }
    }

    private val sharedPrefDefault: SharedPreferences

    private val sharedPreferSigning: SharedPreferences

    init {
        sharedPrefDefault = App.app.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
        sharedPreferSigning = App.app.getSharedPreferences(
                SIGNING_ING_SP_NAME, Context.MODE_PRIVATE
        )
    }

    /**
     * 保存登录userId
     */
    fun saveLoginUserId(userId: String) {
        with(sharedPrefDefault.edit()) {
            putString(USER_ID_KEY, userId)
            apply()
        }
    }

    /**
     * 读取登录UserId
     */
    fun readLoginUserId(): String {
        return sharedPrefDefault.getString(USER_ID_KEY, "")
    }

    /**
     * 删除登录UserId
     */
    fun deleteLoginUserId() {
        with(sharedPrefDefault.edit()) {
            remove(USER_ID_KEY)
            apply()
        }
    }

    /**
     * 保存用户登录状态
     */
    fun saveLoginStates(@LoginStates states: Int) {
        with(sharedPrefDefault.edit()) {
            putInt(USER_LOGIN_STATES, states)
            apply()
        }
    }

    /**
     * 读取用户登录状态
     */
    fun readLoginStates(): Int {
        return sharedPrefDefault.getInt(USER_LOGIN_STATES, LoginStates.loginDefault)
    }

    /**
     * 删除用户登录状态
     */
    fun deleteLoginStates() {
        with(sharedPrefDefault.edit()) {
            remove(USER_LOGIN_STATES)
            apply()
        }
    }

    /**
     * 保存签到范围设置
     */
    fun saveSigningSetting(setting: SigningSetting) {
        with(sharedPrefDefault.edit()) {
            putString(SIGNING_LATITUDE_KEY, setting.latitude.toString())
            putString(SIGNING_LONGITUDE_KEY, setting.longitude.toString())
            putString(SIGNING_RADIUS_KEY, setting.radius.toString())
            apply()
        }
    }

    /**
     * 读取签到范围设置
     */
    fun readSigningSetting(): SigningSetting {
        val latitude = sharedPrefDefault.getString(SIGNING_LATITUDE_KEY, "0.0").toDouble()
        val longitude = sharedPrefDefault.getString(SIGNING_LONGITUDE_KEY, "0.0").toDouble()
        val radius = sharedPrefDefault.getString(SIGNING_RADIUS_KEY, "100.0").toDouble()
        return SigningSetting(latitude, longitude, radius)
    }

    /**
     * 保存签到定位检测时间间隔
     */
    fun saveSigningInterval(value: String) {
        with(sharedPrefDefault.edit()) {
            putString(SIGNING_INTERVAL_KEY, value)
            apply()
        }
    }

    /**
     * 读取签到定位检测时间间隔
     */
    fun readSigningInterval(): String {
        return sharedPrefDefault.getString(SIGNING_INTERVAL_KEY, "10")
    }

    /**
     * 保存学习时长
     */
    fun saveStudyTime(value: String) {
        with(sharedPrefDefault.edit()) {
            putString(STUDY_TIME_KEY, value)
            apply()
        }
    }

    /**
     * 读取学习时长
     */
    fun readStudyTime(): String {
        return sharedPrefDefault.getString(STUDY_TIME_KEY, "30")
    }

    /**
     * 保存当前正在计时的记录ID
     */
    fun saveSigningDataId(id: Long) {
        with(sharedPreferSigning.edit()) {
            putLong(SIGNING_ING_ID_KEY, id)
            apply()
        }
    }

    fun readSigningDataId(): Long {
        return sharedPreferSigning.getLong(SIGNING_ING_ID_KEY, -1L)
    }

    fun deleteSigningDataId() {
        with(sharedPreferSigning.edit()) {
            clear()
            apply()
        }
    }

}